﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using System.Text;
using System.Security.Cryptography;
using System.Data;


public partial class Dang_Ky : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    CustomersBLL cus = new CustomersBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Reset();
            lbSelected.Text = "0 item(s)";
            LoadCountItem();
        }
    }

    void LoadCountItem()
    {
        DataTable dtCart = Session["Cart"] as DataTable;
        if (dtCart.Rows.Count > 0)
        {
            lbSelected.Text = dtCart.Compute("SUM(Quantity)", "").ToString() + " item(s)";
        }
    }

    void Reset()
    {
        txtCustomerName.Text = string.Empty;
        txtUserName.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtConfirmPassword.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtCountry.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmail.Text = string.Empty;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Reset();
    }

    string MaHoa(string chuoiCanMaHoa)
    {
        byte[] data = Encoding.ASCII.GetBytes(chuoiCanMaHoa);
        SHA512 sha5 = new SHA512Managed();
        byte[] result = sha5.ComputeHash(data);
        string chuoiMaHoa = Encoding.ASCII.GetString(result);
        return chuoiMaHoa;
    }

    bool UserNameExist(string UserName)
    {
        var danhsach = db.customers.SingleOrDefault
            (cu => cu.username.Equals(UserName));
        if (danhsach != null)
            return true;
        return false;
    }

    protected void btnSignUp_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (UserNameExist(txtUserName.Text) == true)
            {
                lblThongBao.Text = "<br />Tên tài khoản đã tồn tại. Vui lòng đổi tên.";
                return;
            }
            customer c = new customer
            {
                customername = txtCustomerName.Text,
                username = txtUserName.Text,
                password = MaHoa(txtPassword.Text),
                address = txtAddress.Text,
                city = txtCity.Text,
                signupdate = DateTime.Now.Date,
                country = txtCountry.Text,
                phone = txtPhone.Text,
                email = txtEmail.Text
            };
            cus.Customer_Insert(c);
            db.SubmitChanges();
            Reset();
            Session["Username"] = c.username;
            lblThongBao.Text = "<br /> Đăng ký thành công";
        }
        catch
        {
            lblThongBao.Text = "<br />Đăng ký thất bại";   
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Tim-kiem.aspx?tk=" + txtsearch.Text);
    }
}